package com.eaample.wexcle.demos.web.service;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyuncs.auth.DefaultCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.eaample.wexcle.demos.web.config.ConstandPropertiesUtils;
import com.eaample.wexcle.demos.web.service.impl.OssServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

@Service
public class OssService implements OssServiceImpl {

    //工具类获取值
    String endpoint = ConstandPropertiesUtils.END_POIND;
    String accessKeyId = ConstandPropertiesUtils.ACCESS_KEY_ID;
    String accessKeySecret = ConstandPropertiesUtils.ACCESS_KEY_SECRET;
    String bucketName = ConstandPropertiesUtils.BUCKET_NAME;





    //file形式上传文件到oss
    @Override
    public String uploadFileAvatar(MultipartFile file)  {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 创建一个ClientConfiguration对象，并设置超时时间
        ClientConfiguration conf = new ClientConfiguration();
        conf.setSocketTimeout(50000); // 设置socket超时时间为50000毫秒
        conf.setConnectionTimeout(10000); // 设置连接超时时间为10000毫秒

        try {
            //创建OSS实列

            //上传文件流
            InputStream inputStream = file.getInputStream();
            //获取文件的名称
            String fileName = file.getOriginalFilename();
            //1.在文件名称里面添加随机唯一的值
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            fileName = uuid+fileName;
            //2.把上传的文件按日期分类2024/6/16/01.jpg
            String datePath = new DateTime().toString("yyyy/MM/dd");
            //3.拼接
            fileName = datePath+"/"+fileName;
            //调用oss方法实现上传
            //第一个参数 bucketName
            //第二个参数 上传到oss文件的路径及名称fileName
            //第三个参数 上传文件的输入流inputStream
            ossClient.putObject(bucketName,fileName,inputStream);
            //关闭ossClient
            ossClient.shutdown();
            //把上传后文件路径返回
            //需要把上传到阿里云oss路径手动拼接出来URL
            //https://edu-wzh-1010.oss-cn-beijing.aliyuncs.com/003304-1704990784db12.jpg
            String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
            return url;
        }catch (Exception e){
         e.printStackTrace();
         return null;
        }


    }

    @Override
    public String uploadBytes(String address) {

        String url = null;
        String fileName = null;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            String[] split = address.split("\\\\");
            for (int i = 0; i < split.length; i++) {
                System.out.println(split[i] + "-----------------");
            }
            fileName = split[split.length - 1];

            String datePath = new DateTime().toString("yyyy/MM/dd");
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;
            fileName = datePath + "/" + fileName;

            url = fileName;
            fileName = "https://" + bucketName + "." + endpoint + "/" + url;
            // 读取文件
            FileInputStream inputStream = new FileInputStream(address);

            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, url, inputStream);

            // 上传文件
            ossClient.putObject(putObjectRequest);

            System.out.println("文件上传成功");

            // 关闭inputStream
            inputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return fileName;
    }


}
